package com.waidi.clothespad.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.waidi.clothespad.common.Config;
import com.waidi.clothespad.common.FaultInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by ll on 2018/7/1.
 */

public class BaseInfoAdapter extends BaseAdapter {

    private Context context;
    private String TAG ="BaseInfoAdapter:";

    public  ArrayList<FaultInfo> faultInfoArrayList = new ArrayList<>();

    public String[] fault_name =new String[100];
    public String[] fault_code =new String[100];
    public List<String> salaryInfo = new LinkedList<>();


    private int count = 100;
    private int clickTemp = -1;//标识被选择的item
    private int clickTemp1 = -1;
    private int[] clickedList=new int[100];//这个数组用来存放item的点击状态
    public  int Selectednum = 0;
    private boolean click = false;

    public BaseInfoAdapter(Context context, String in)
    {
        count = 20;
        this.context = context;
        //if(loadFaultInfo()==false)
        {
            for (int i = 0; i < count; i++)
            {
                fault_name[i]="1000"+i;
                fault_code[i] =Integer.toString(i);
/*                FaultInfo tmp = new FaultInfo();
                tmp.fault_name = "掉线、漏针、拷边" + i;
                tmp.fault_code = Integer.toString(i);
                tmp.clickedList = 0;
                faultInfoArrayList.add(tmp);*/
            }


        }

        for (int i =0;i<count;i++)
        {
            clickedList[i] = 0;      //初始化item点击状态的数组

        }

    }

    public boolean loadFaultInfo()
    {

        File faultinfotxt = new File(Config.faultinfotxt);


        if(faultinfotxt.exists())
        {
            try
            {
                FileInputStream in = new FileInputStream(faultinfotxt);
                int len = 0;
                byte[] buf = new byte[1024];
                String  strbuf;
                String  strjson="";
                while((len=in.read(buf))!=-1)
                {
                    strbuf = new String(buf,0,len);
                    strjson = strjson + strbuf;
                }
                in.close();
                if(strjson==null)
                    return false;

                JSONObject jsonObj = new JSONObject(strjson);
                if(jsonObj==null)
                    return false;

                JSONArray qcArray = jsonObj.getJSONArray("data");
                if(qcArray==null)
                    return false;

                for (int i=0;i<qcArray.length();i++)
                {
                    JSONObject s= (JSONObject)qcArray.get(i);
                    FaultInfo tmp = new FaultInfo();
                    tmp.fault_name =s.getString("fault_name");
                    tmp.fault_code = s.getString("fault_code");
                    tmp.clickedList = 0;
                    faultInfoArrayList.add(tmp);
                }
                return true;
            }
            catch (JSONException je)
            {
                je.printStackTrace();
                return false;
            }
            catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        else {
            return  false;
        }

    }


    public void setSeclection(int position)
    {
        click = true ;
        clickTemp = position;
        Log.e(TAG,"9999");
    }

    public void clearClickList()
    {
       //for(int i =0;i<faultInfoArrayList.size();i++)
        {
            //clickedList[i] = 0;//初始化item点击状态的数组
          //  faultInfoArrayList

        }
        clickTemp = -1;
    }

    @Override
    public int getCount()
    {

        //return faultInfoArrayList.size();
        return count;
    }

    @Override
    public Object getItem(int position)
    {

        //return faultInfoArrayList.get(position).fault_name;
        return fault_name[position];

    }


    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView tv;
        if (convertView == null)
        {
            tv = new TextView(context);
            tv.setGravity(Gravity.CENTER);
            tv.setLayoutParams(new GridView.LayoutParams(195, 60));
        }
        else
        {
            tv = (TextView) convertView;
        }

        tv.setText(fault_name[position]);
        //tv.setText(faultInfoArrayList.get(position).fault_name);




        Log.i(TAG,clickedList[position] +"#"+ Integer.toString(clickTemp)+"#"+Integer.toString(position));

/*        if(clickTemp==position&&click==true)
        {    //根据点击的Item当前状态设置背景

            //if (faultInfoArrayList.get(position).clickedList==0)
            if(clickedList[position]==0)
            {
                convertView.setBackgroundColor(Color.rgb(222,233,12));
                Log.e(TAG,"8888");

*//*                FaultInfo faultInfo = faultInfoArrayList.get(position);

                faultInfo.clickedList = 1;
                faultInfoArrayList.set(position,faultInfo);*//*
                clickedList[position] =1;
                Selectednum++;
            }
            else
            {
                //convertView.setBackgroundColor(Color.TRANSPARENT);
                //FaultInfo faultInfo = faultInfoArrayList.get(position);
                //faultInfo.clickedList = 0;
                clickedList[position] =0;


                if(Selectednum>0)
                    Selectednum--;
            }
            click=false;
        }*/

        Log.i("BaseInfoAdapter",Integer.toString(Selectednum));
        return tv;

    }




}
